home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
ftp.mactech.com 2010
/
ftp.mactech.com.tar
/
ftp.mactech.com
/
machack
/
Hacks97
/
VUlites.sit
/
VUlites
/
source code
/
VUlites.cpp
next >
Wrap
C/C++ Source or Header
|
1997-06-28
|
6KB
|
331 lines
#include "VUlites.h"
/*
stuff we'll need to check and set up:
PowerPC architecture
sound manager version
auto gain off
asynchronous?
uncompressed
continuous recording on
44.1/16
"quality"
*/
long mikeRefNum; // the RefNum of our input device
SPB paramBlock; // sound parameter block
Ptr soundBuffer;
//Ptr soundBuffer2;
long soundSetupBuffer[5];
long lowThresh = 0x2000;
long midThresh = 0x4800;
long hiThresh = 0x6000;
long lowCount = 0;
long midCount = 0;
long hiCount = 0;
long number = 0;
bool quit = false;
bool requestToQuit = false;
SICompletionUPP lightsUPP;
long oldLightSetting = 0;
bool adbDone = false;
ADBCompletionUPP adbComp;
DeferredTaskUPP defTask;
DeferredTask taskRec;
//int currentBuff = 0;
void main(void)
{
// ----- Variables
OSErr error; // to catch an error we go
EventRecord junkEvent;
// ----- Code
// Allocate buffers
soundBuffer = NewPtr(kBufferSize);
if (soundBuffer == NULL || MemError())
{
//cout << "Cannot allocate buffer." << endl;
return;
}
/*
soundBuffer2 = NewPtr(kBufferSize);
if (soundBuffer == NULL || MemError())
{
//cout << "Cannot allocate buffer." << endl;
return;
}
*/
// Get our microphone
error = SPBOpenDevice("\p", // The name of the device. We want the default one.
siWritePermission, // Permission? Do we need permission?
&mikeRefNum); // And a refNum for the input device
//cout << "The refNum is " << mikeRefNum << "." << endl;
if (error != noErr)
{
//cout << "Error: " << static_cast<long>(error) << "." << endl;
return;
}
if (mikeRefNum == 0 || error != noErr)
return;
// Set up stuff
soundSetupBuffer[0] = rate44khz;
error = SPBSetDeviceInfo(mikeRefNum, siSampleRate, soundSetupBuffer);
if (error != noErr)
{
//cout << "Error: " << static_cast<long>(error) << "." << endl;
return;
}
soundSetupBuffer[0] = 'NONE';
error = SPBSetDeviceInfo(mikeRefNum, siCompressionType, soundSetupBuffer);
if (error != noErr)
{
//cout << "Error: " << static_cast<long>(error) << "." << endl;
return;
}
soundSetupBuffer[0] = 1 << 16;
error = SPBSetDeviceInfo(mikeRefNum, siNumberChannels, soundSetupBuffer);
if (error != noErr)
{
//cout << "Error: " << static_cast<long>(error) << "." << endl;
return;
}
soundSetupBuffer[0] = 16 << 16;
error = SPBSetDeviceInfo(mikeRefNum, siSampleSize, soundSetupBuffer);
if (error != noErr)
{
//cout << "Error: " << static_cast<long>(error) << "." << endl;
return;
}
// Call, sleep
// Make up all these damned UPPs
lightsUPP = NewSICompletionProc(lights);
adbComp = NewADBCompletionProc(ADBComp);
defTask = NewDeferredTaskProc(DefTask);
// Set up the parameter block
paramBlock.inRefNum = mikeRefNum;
paramBlock.count = kBufferSize;
paramBlock.milliseconds = 0;
paramBlock.bufferLength = kBufferSize;
paramBlock.bufferPtr = soundBuffer;
paramBlock.completionRoutine = lightsUPP;
paramBlock.interruptRoutine = NULL;
paramBlock.userLong = 0xd15c0L;
paramBlock.error = noErr;
paramBlock.unused1 = 0;
// Play that funky music, white boy!
SPBRecord(¶mBlock,
TRUE); // asynchronous, please
while (quit == false)
{
WaitNextEvent (0, &junkEvent, 0x60, nil);
}
// Release the mike to someone else
error = SPBCloseDevice(mikeRefNum);
//if (error != noErr)
// cout << "Error: " << static_cast<long>(error) << "." << endl;
// Kill the UPPs
DisposeRoutineDescriptor(lightsUPP);
DisposeRoutineDescriptor(adbComp);
DisposeRoutineDescriptor(defTask);
}
asm long abs (long a)
{
srawi r4, r3, 31
add r5, r4, r3
xor r3, r5, r4
}
void lights (SPBPtr ignore1, Ptr ignore2, short ignore3, long ignore4)
{
//DebugStr("\pLights;g");
long newLightSetting = 7;
if (requestToQuit)
{
quit = true;
return;
}
for (short *i = (short *)soundBuffer;
i < (short *)(soundBuffer + kBufferSize/sizeof(short));
i++)
{
if (abs(*i) < lowThresh)
{
newLightSetting &= 0x07;
}
else if (abs(*i) < midThresh)
{
newLightSetting &= 0x06;
}
else if (abs(*i) < hiThresh)
{
newLightSetting &= 0x04;
}
else
{
newLightSetting &= 0x00;
}
}
switch (newLightSetting)
{
case 0:
hiCount++;
break;
case 4:
midCount++;
break;
case 6:
lowCount++;
break;
}
number++;
//Re-evaluate the thresholds
if (number >= reevalThresh)
{
if (hiCount < (reevalThresh / 4 * 3))
{
hiThresh -= 0x100;
}
else if (hiCount > (reevalThresh / 4 * 3))
{
hiThresh += 0x100;
}
if (midCount < (reevalThresh / 2))
{
midThresh -= 0x100;
}
else if (midCount > (reevalThresh / 2))
{
midThresh += 0x100;
}
if (lowCount < (reevalThresh / 4))
{
lowThresh -= 0x100;
}
else if (lowCount > (reevalThresh / 4))
{
lowThresh += 0x100;
}
lowCount = 0;
midCount = 0;
hiCount = 0;
number = 0;
}
// New light setting? Then update lights
if (newLightSetting != oldLightSetting)
{
oldLightSetting = newLightSetting;
//Make deferred task
taskRec.qType = dtQType;
taskRec.dtAddr = defTask;
taskRec.dtReserved = 0;
DTInstall (&taskRec);
}
// Set up the parameter block
paramBlock.inRefNum = mikeRefNum;
paramBlock.count = kBufferSize;
paramBlock.milliseconds = 0;
paramBlock.bufferLength = kBufferSize;
paramBlock.bufferPtr = soundBuffer;
paramBlock.completionRoutine = lightsUPP;
paramBlock.interruptRoutine = NULL;
paramBlock.userLong = 0xd15c0L;
paramBlock.error = noErr;
paramBlock.unused1 = 0;
// Play that funky music, white boy!
SPBRecord(¶mBlock,
TRUE); // asynchronous, please
}
void ADBComp (Ptr ignore1, Ptr ignore2, long ignore3)
{
adbDone = true;
}
void DefTask (long ignore1)
{
//DebugStr("\pDefTask;g");
char RegisterData[5];
unsigned char command;
OSErr myErr;
// Read
adbDone = false;
RegisterData[0] = 2;
command = (0x02 * 16) + 0x0C + 0x02;
myErr = ADBOp(nil, adbComp, RegisterData, command);
do {} while (adbDone == 0);
if (myErr != noErr)
{
requestToQuit = true;
return;
}
// Set
adbDone = false;
RegisterData[2] &= (0xff - 0x07);
RegisterData[2] |= oldLightSetting;
command = (0x02 * 16) + 0x08 + 0x02;
myErr = ADBOp(nil, adbComp, RegisterData, command);
do {} while (adbDone == 0);
if (myErr != noErr)
{
requestToQuit = true;
return;
}
}